package com.dayuanit.dy14.enter.dy14enter1024.service.impl;

import com.dayuanit.dy14.enter.dy14enter1024.dto.GroupDTO;
import com.dayuanit.dy14.enter.dy14enter1024.dto.UserDTO;
import com.dayuanit.dy14.enter.dy14enter1024.entity.Group;
import com.dayuanit.dy14.enter.dy14enter1024.entity.User;
import com.dayuanit.dy14.enter.dy14enter1024.enums.GroupStatusEnum;
import com.dayuanit.dy14.enter.dy14enter1024.exception.BizException;
import com.dayuanit.dy14.enter.dy14enter1024.mapper.GroupMapper;
import com.dayuanit.dy14.enter.dy14enter1024.mapper.UserMapper;
import com.dayuanit.dy14.enter.dy14enter1024.service.GroupService;
import com.dayuanit.dy14.enter.dy14enter1024.service.base.BaseService;
import com.dayuanit.dy14.enter.dy14enter1024.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GroupServiceImpl extends BaseService implements GroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void groupUser() {
        List<User> users = userMapper.listUser4Group();
        int total = users.size();

        if (total < 2) {
            throw new BizException("分数过少，分组失败");
        }

        int groupCount = 2;

        if (total > 4 && total <= 20) {
            groupCount = 3;
        } else {
            groupCount = 10;
        }

        int currentUserIndex = 0;
        Group group = null;
        for (int i=0; i<groupCount; i++) {
            //TODO 创建小组
            group = new Group();
            group.setCreateTime(new Date());
            group.setModifyTime(group.getCreateTime());
            group.setStatus(GroupStatusEnum.已成立.getK());
            int rows = groupMapper.insert(group);
            if (1 != rows) {
                throw new BizException("分组失败");
            }

            //为每个小组分配人员
            int groupUser = total / groupCount;
            for (int k=0; k<groupUser; k++) {
                userMapper.updateGroup(users.get(currentUserIndex++).getId(), group.getId());
            }
        }

        //如果有余 不能整除 则将余下的人都放到最后一组。
        if (total % groupCount != 0) {
            for (int k=0; k<total % groupCount; k++) {
                userMapper.updateGroup(users.get(currentUserIndex++).getId(), group.getId());
            }
        }

    }

    @Override
    public List<GroupDTO> listTop10Group() {
        return groupMapper.listTop10Group();
    }

    @Override
    public PageUtils queryGroup(Byte status, Integer id, Integer currentPage) {

        int total = groupMapper.countGroup(status, id);
        PageUtils pageUtils = PageUtils.build(currentPage, total).init();

        List<GroupDTO> groupDTOS = groupMapper.listGroup(status, id, pageUtils.offset(), PageUtils.pre_page_number);

        pageUtils.setData(groupDTOS);
        return pageUtils;
    }

    @Override
    public List<UserDTO> listGroupDetail(Integer groupId) {
        List<User> users = userMapper.listUserByGroupId(groupId);
        List<UserDTO> dtos = new ArrayList<>(users.size());

        for (User user : users) {
            UserDTO userDTO = new UserDTO();
            dtos.add(userDTO);

            userDTO.setEmail(user.getEmail());
            userDTO.setUsername(user.getUsername());
            userDTO.setUserId(user.getId());
        }
        return dtos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dismiss(int groupId) {
        //TODO 更新 删除的时候 一定要先将数据查出来
        Group group = groupMapper.selectByPrimaryKey(groupId);
        if (null == group) {
            throw new BizException("小组不存在");
        }

        if (group.getStatus() == GroupStatusEnum.已解散.getK()) {
            throw new BizException("不能重复解散");
        }

        int rows = groupMapper.updateGroupStatus(groupId, GroupStatusEnum.已解散.getK(), group.getStatus());
        if (1 != rows) {
            throw new BizException("解散失败");
        }
    }
}
